package com.xmy.cultivate.controller;

import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.views.ChaPlan;
import com.xmy.cultivate.entity.views.MathChaPlan;
import com.xmy.cultivate.entity.views.ResStudentGrade;
import com.xmy.cultivate.feign.math.IFeiMathFeignClient;
import com.xmy.cultivate.feign.physics_outside.IPhysicsFeignClient;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IRecordCourseService;
import com.xmy.cultivate.service.IStudentGradeService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/studyPlan")
public class StudyPlanController {

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IStudentGradeService iStudentGradeService;

    @Autowired
    IFeiMathFeignClient iFeiMathFeignClient;

    @Autowired
    IPhysicsFeignClient iPhysicsFeignClient;

    @Autowired
    IRecordCourseService iRecordCourseService;

    @GetMapping("/getPassRadeDetailForStudentId")
    public Result getPassRadeDetailForStudentId(
            @RequestParam(value = "studentId", required = false) String studentId,
            @RequestParam(value = "subjectsId", required = false) String subjectsId) {

        Integer lessonType = 1;
        Quarter quarterNow = iQuarterService.getNowQuarter();
        String dateTimeNow = LocalDateTime.now().withNano(0).toString();

        // 已经上的精品课课次
        String yetLessonNumStr = iRecordCourseService.getYetLessonNumStr(quarterNow.getYearPart(), quarterNow.getNum(), Long.parseLong(subjectsId), lessonType, Long.parseLong(studentId), dateTimeNow);

        List<MathChaPlan> chaPlanList = Collections.emptyList();
        try {
            if (subjectsId.equals("2")) {
                chaPlanList = iFeiMathFeignClient.getPassRadeDetailForStudentId(quarterNow.getYearPart().toString(), quarterNow.getNum().toString(), studentId, subjectsId, yetLessonNumStr);
            } else if (subjectsId.equals("3")) {
                chaPlanList = iPhysicsFeignClient.getPassRadeDetailForStudentId(quarterNow.getYearPart().toString(), quarterNow.getNum().toString(), studentId, subjectsId, yetLessonNumStr);
            }
        } catch (Exception e) {
        }

        ResStudentGrade studentGrade = iStudentGradeService.getStudentGradeClassType(quarterNow.getYearPart(), quarterNow.getNum(), Long.parseLong(subjectsId), Long.parseLong(studentId), lessonType);
        for (MathChaPlan mathChaPlan : chaPlanList) {
            if (studentGrade != null) {
                mathChaPlan.setYearClassName(CommonUtil.getYearClassNameForId(studentGrade.getYearClassId()));
                mathChaPlan.setTeacherName(studentGrade.getTeacherName());
            } else {
                mathChaPlan.setYearClassName("");
                mathChaPlan.setTeacherName("");
            }
        }

        return new Result(Code.OK, chaPlanList, "获取成功");
    }
}
